package com.barnabot.bigo.util;
// step 7
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

//import org.codehaus.jackson.map.deser.ThrowableDeserializer;
import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.classic.Session;
//import org.hibernate.Session;

import com.barnabot.bigo.util.HibernateUtil;

// Filter is servlet type
public class ConexaoHibernateFilter implements Filter{
	
    public void destroy() {  
    }  
      
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {  
          
  
        Session session = HibernateUtil.getSession();  
  
  
        try {  
              
            session.beginTransaction();  
              
            ((HttpServletRequest) req).setAttribute("sessaoHibernate", session);  
            fc.doFilter(req, res);  
              
            session.getTransaction().commit();  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            session.close();  
        }  
    }  
  
    public void init(FilterConfig arg0) throws ServletException {  
    }  
    
}
